/**
 * @file sequential_stack.cpp
 * @brief 顺序栈的实现
 * @author Xavier ZXY (zxy_xavier@163.com)
 * @version : 1.0
 * @date 2022-04-24 02:06
 *
 */

#include "sequential_stack.h"
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <io.h>
#include <math.h>
#include <time.h>

using namespace std;

int main()
{
    SqStack s;
    if (InitStack(&s) == OK)
    {
        for (int i = 0; i <= 10; i++)
        {
            Push(&s, i);
        }
    }

    cout << "栈中的元素依次为: ";
    StackTraverse(s);
    cout << endl;
    int e;
    Pop(&s, &e);
    cout << "弹出栈顶元素: " << e << endl;
    printf("栈空否：%d(1:空 0:否)\n", StackIsEmpty(s));
    GetTop(s, &e);
    printf("栈顶元素 e=%d 栈的长度为%d\n", e, StackLength(s));
    ClearStack(&s);
    printf("清空栈后，栈空否：%d(1:空 0:否)\n", StackIsEmpty(s));

    return 0;
}

//显示元素
Status visit(SElemType c)
{
    cout << c << endl;
    return OK;
}

//初始化空栈
Status InitStack(SqStack *S)
{
    S->top = -1;
    return OK;
}

//把S置为空栈
Status ClearStack(SqStack *S)
{
    S->top = -1;
    return OK;
}

//判断栈是否为空栈
Status StackIsEmpty(SqStack S)
{
    if (S.top == -1)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

//返回S的元素个数，即栈的长度
int StackLength(SqStack S)
{
    return S.top + 1;
}

//返回栈顶元素
Status GetTop(SqStack S, SElemType *e)
{
    if (S.top == -1)
    {
        return ERROR;
    }
    else
    {
        *e = S.data[S.top];
    }

    return OK;
}

//向S中插入新的栈顶元素
Status Push(SqStack *S, SElemType e)
{
    if (S->top == MAXSIZE - 1)
    {
        return ERROR;
    }
    S->top++;
    S->data[S->top] = e;
    return OK;
}

//弹出栈顶元素保留到e
Status Pop(SqStack *S, SElemType *e)
{
    if (S->top == -1)
    {
        return ERROR;
    }
    *e = S->data[S->top];
    S->top--;
    return OK;
}

//查看栈中的所有元素
Status StackTraverse(SqStack S)
{
    for (int i = 0; i <= S.top; i++)
    {
        visit(S.data[i]);
    }

    return OK;
}